home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / snip9_91.arc / RG_SSORT.C < prev    next >
C/C++ Source or Header  |  1991-09-17  |  1KB  |  45 lines

  1. /*
  2. ** Here is a version of Shell sort that is compatible with qsort() 
  3. ** and about as tight and fast as you're going to get in C.
  4. */
  5.  
  6. /*
  7. ** ssort()  --  Shell sort   R. Gardner   public domain   5/90
  8. */
  9.  
  10. ssort (base, nel, width, comp)
  11. char *base;
  12. unsigned int nel, width;
  13. int (*comp)();
  14. {
  15.       unsigned int wnel, gap, wgap, i, j, k;
  16.       char *a, *b, tmp;
  17.  
  18.       wnel = width * nel;
  19.       for (gap = 0; ++gap < nel;)
  20.             gap *= 3;
  21.       while ( gap /= 3 )
  22.       {
  23.             wgap = width * gap;
  24.             for (i = wgap; i < wnel; i += width)
  25.             {
  26.                   for (j = i - wgap; ;j -= wgap)
  27.                   {
  28.                         a = j + base;
  29.                         b = a + wgap;
  30.                         if ( (*comp)(a, b) <= 0 )
  31.                               break;
  32.                         k = width;
  33.                         do
  34.                         {
  35.                               tmp = *a;
  36.                               *a++ = *b;
  37.                               *b++ = tmp;
  38.                         } while ( --k );
  39.                         if (j < wgap)
  40.                               break;
  41.                   }
  42.             }
  43.       }
  44. }
  45.